import torch
import torch.nn as nn
import torch.nn.init as init
import torch.nn.functional as F
import math
from attention.self_attention import SelfAttentionDot


class LipNet(nn.Module):
    def __init__(self, C,rnn_size=256,c_dropout=0.2,o_dropout=0.3):
        super(LipNet, self).__init__()

        self.conv = nn.Sequential(
            # nn.Conv3d(1, 16, kernel_size=(3, 5, 5), stride=(1, 2, 2), padding=(1, 2, 2)),
            nn.Conv3d(1, 32, kernel_size=(3, 5, 5), stride=(1, 2, 2), padding=(1, 2, 2), bias=False),
            nn.BatchNorm3d(32),
            nn.ReLU(True),
            nn.MaxPool3d(kernel_size=(1, 2, 2), stride=(1, 2, 2), padding=(0, 1, 0)),
            nn.Dropout3d(c_dropout)
        )

        self.conv1 = nn.Sequential(
            nn.Conv3d(32, 32, kernel_size=(3, 5, 5), stride=(1, 1, 1), padding=(1, 2, 2), bias=False),
            nn.Dropout3d(c_dropout)
        )
        self.conv1_pool = nn.Sequential(
            nn.BatchNorm3d(64),
            nn.ReLU(True),
            nn.MaxPool3d(kernel_size=(1, 3, 3), stride=(1, 2, 2))
        )

        self.conv2 = nn.Sequential(
            nn.Conv3d(64, 64, kernel_size=(3, 3, 3), stride=(1, 1, 1), padding=(1, 1, 1), bias=False),
            nn.BatchNorm3d(64),
            nn.ReLU(True),
            nn.MaxPool3d(kernel_size=(1, 2, 2), stride=(1, 2, 2)),
            nn.Dropout3d(c_dropout)
        )


        self.attention = SelfAttentionDot(512,512)
        # T B C*H*W
        self.gru1 = nn.GRU(64 * 3 * 6, rnn_size, 1, bidirectional=True,batch_first=True)
        self.drp1 = nn.Dropout(o_dropout)
        # T B F
        self.gru2 = nn.GRU(rnn_size*2, rnn_size, 1,bidirectional=True,batch_first=True)
        self.drp2 = nn.Dropout(o_dropout)
        self.rule = nn.ReLU()
        # T B V
        self.pred = nn.Linear(rnn_size * 2 * 2, C)
        self.bn_norm1 = nn.BatchNorm1d(20, momentum=0.5)
        self.bn_norm2 = nn.BatchNorm1d(512*2, momentum=0.5)

        # initialisations
        for m in self.conv.modules():
            if isinstance(m, nn.Conv3d):
                init.kaiming_normal_(m.weight, nonlinearity='relu')
                # init.constant_(m.bias, 0)

        init.kaiming_normal_(self.pred.weight, nonlinearity='sigmoid')
        init.constant_(self.pred.bias, 0)

        for m in (self.gru1,self.gru2):
            stdv = math.sqrt(2 / (64 * 3 * 6 + rnn_size))
            for i in range(0, rnn_size * 3, rnn_size):
                init.uniform_(m.weight_ih_l0[i: i + rnn_size],
                              -math.sqrt(3) * stdv, math.sqrt(3) * stdv)
                init.orthogonal_(m.weight_hh_l0[i: i + rnn_size])
                init.constant_(m.bias_ih_l0[i: i + rnn_size], 0)
                init.uniform_(m.weight_ih_l0_reverse[i: i + rnn_size],
                              -math.sqrt(3) * stdv, math.sqrt(3) * stdv)
                init.orthogonal_(m.weight_hh_l0_reverse[i: i + rnn_size])
                init.constant_(m.bias_ih_l0_reverse[i: i + rnn_size], 0)

    def forward(self, x):
        self.gru1.flatten_parameters()
        self.gru2.flatten_parameters()

        x = self.conv(x)  # B C T H W
        conv1_out = self.conv1(x)
        x = torch.cat((x,conv1_out),dim=1)

        x = self.conv1_pool(x)

        x = self.conv2(x)



        x = x.permute(0, 2, 1, 3, 4).contiguous()  # T B C H W
        x = x.view(x.size(0), x.size(1), -1)


        x,hidden = self.gru1(x)
        x = self.drp1(x)

        x, _ = self.gru2(x,hidden)

        att_out = self.attention(x)
        pool_out = x.transpose(1,2)
        pool_out = F.max_pool1d(pool_out, pool_out.size(2)).squeeze(2)
        out = torch.cat((att_out,pool_out),dim=1)
        out = self.bn_norm2(out)
        out = self.drp2(out)
        out = self.pred(out).log_softmax(-1)
        # x = F.log_softmax(x, dim=1)

        return out


class LipSeqLoss(nn.Module):
    def __init__(self):
        super(LipSeqLoss, self).__init__()
        self.criterion = nn.NLLLoss(reduction='none')

    def forward(self, input, length, target):
        loss = []
        transposed = input.transpose(0, 1).contiguous()
        # transposed = input
        for i in range(transposed.size(0)):
            loss.append(self.criterion(transposed[i,], target.squeeze(1)).unsqueeze(1))
        loss = torch.cat(loss, 1)

        # GPU version
        mask = torch.zeros(loss.size(0), loss.size(1)).float().cuda()
        # Cpu version
        #         mask = torch.zeros(loss.size(0), loss.size(1)).float()

        for i in range(length.size(0)):
            L = min(mask.size(1), length[i])
            mask[i, L - 1] = 1.0
        loss = (loss * mask).sum() / mask.sum()
        return loss




if __name__ == '__main__':

    from DataLoader.Picture_Process import file_2_allpicture
    path = r"D:\XW_Bank\LipRecognition\train\lip_100_50_train\000c43e99bdceb93a39e729ffc38ac2a"
    input_p,pct_len = file_2_allpicture(path)
    model = LipNet(313,256).cuda()
    src = input_p.cuda()
    output = model(src)
    print(output.shape)
    length = torch.LongTensor([9]).cuda()
    print()

    # loss_func = LipSeqLoss()
    # loss = loss_func(output, torch.LongTensor([9]).cuda(), torch.LongTensor([[200]]).cuda())
    # print(loss)